package util;

import com.alibaba.fastjson.JSON;
import pojo.ServerMessage;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class StreamUtil<S,R> {
    Class<R> receive;

    public void write(ObjectOutputStream stream, S send) throws IOException {
        stream.writeObject(JSON.toJSONString(send));
    }

    public R read(ObjectInputStream stream) throws IOException, ClassNotFoundException {
        String json = (String) stream.readObject();
        return (R) JSON.parseObject(json,receive);
    }

    public StreamUtil(Class<R> receive) {
        this.receive = receive;
    }
}
